package com.jiazhong.test;

import com.jiazhong.testone.mapper.UserMapper;
import com.jiazhong.testone.pojo.User;
import com.jiazhong.testtwo.mapper.BrandMapper;
import com.jiazhong.testtwo.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther:shuXin
 */
public class MybatisTest01 {
    @Test
    public void selectAll() throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = brandMapper.selectAll();
        System.out.println(brands);
        sqlSession.close();
    }

    @Test
    public void selectById() throws Exception {
        //接受id的值
        int id = 7;
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        Brand brand = brandMapper.selectById(id);
        System.out.println(brand);
        sqlSession.close();
    }

    @Test
    public void testSelectByCondition() throws Exception {
        //接受传进来的每一个值
        int status = 1;
        String brandName = "华为";
        String companyName = "华为";
        //给String类型使用模糊查询要使用%进行拼接
        brandName = "%" + brandName + "%";
        companyName = "%" + companyName + "%";


        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //方式一 ：接口方法参数使用 @Param 方式调用的方法
        List<Brand> brands = brandMapper.selectByCondition(status, brandName, companyName);
        //方式二 ：接口方法参数是 实体类对象 方式调用的方法
        //封装对象
        /*Brand brand = new Brand();
        brand.setStatus(status);
        //brand.setBrandName(brandName);
        //brand.setCompanyName(companyName);
        List<Brand> brands = brandMapper.selectByCondition(brand);*/
        //方式三 ：接口方法参数是 map集合对象 方式调用的方法
        /*Map map = new HashMap();
        map.put("status", status);
        map.put("brandName", brandName);
        map.put("companyName", companyName);*/
        //List<Brand> brands = brandMapper.selectByCondition(map);
        System.out.println(brands);
        sqlSession.close();
    }

    @Test
    public void selectByConditionSingle() throws Exception {
        //接受传进来的每一个值
        int status = 1;
        String brandName = "华为";
        String companyName = "华为";
        //给String类型使用模糊查询要使用%进行拼接
        brandName = "%" + brandName + "%";
        companyName = "%" + companyName + "%";
        //封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        //brand.setCompanyName(companyName);
        //brand.setBrandName(brandName);
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = brandMapper.selectByConditionSingle(brand);
        System.out.println(brands);
        sqlSession.close();
    }

    @Test
    public void add() throws Exception {
        //接受添加进来的每一个值
        int status = 6;
        String brandName = "东方树叶";
        String companyName = "东方树叶科技能力有限公司";
        int ordered = 40;
        String description = "不是很好喝";


        //封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);
        brand.setOrdered(ordered);
        brand.setDescription(description);
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql 也可以在这里设置事务的提交方式
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        brandMapper.add(brand);
        System.out.println(brand.getId());
        //事务的提交
        //sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void update() throws Exception {
        //接受添加进来的每一个值
        int status = 0;
        String brandName = "东方树叶";
        String companyName = "东方树叶科技能力有限公司";
        int ordered = 40;
        String description = "不是很好喝";
        int id = 9;

        //封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        //brand.setCompanyName(companyName);
        //brand.setBrandName(brandName);
        //brand.setOrdered(ordered);
        //brand.setDescription(description);
        brand.setId(id);
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql 也可以在这里设置事务的提交方式
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        brandMapper.update(brand);
        //事务的提交
        //sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void delete() throws Exception {
        //接受添加进来的每一个值
        int id = 10;
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql 也可以在这里设置事务的提交方式
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        brandMapper.delete(id);
        sqlSession.close();
    }

    @Test
    public void deletes() throws Exception {
        //接受添加进来的每一个值
        int[] ids = {5, 7, 8};
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql 也可以在这里设置事务的提交方式
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        brandMapper.deletes(ids);
        sqlSession.close();
    }
}
